const sqlModule = require('../../common/database/sql');
const db = sqlModule.db;
const _ = db.command;
const _l = require('lodash');

const utils = require('../../utils');
const dateToTimestamp = utils.dateToTimestamp;
const timeToMilliseconds = utils.timeToMilliseconds;
const transformRecords = utils.transformRecords;
const hasOverlap = utils.hasOverlap;
//课程模型
const classModel = require('../../models/class/index');

function list(param) {
  return new Promise(async (resolve, reject) => {
    try {
      const result = await classModel.list(param);
      result.records = transformRecords(result.records);
      resolve(result);
    } catch (error) {
      reject(error);
    };
  })
}

function byId(id) {
  return new Promise(async (resolve, reject) => {
    try {
      let todo = await classModel.byId(id);
      todo = transformRecords(todo);
      if (todo._id) {
        resolve(todo);
      } else {
        reject(todo);
      }
    } catch (error) {
      reject(error);
    };
  })
}

function add(param) {
  return new Promise(async (resolve, reject) => {
    try {
      let params = {
        classStudent: [],
        status: 1,
        ...param,
        classDate: dateToTimestamp(param.classDate),
        classTime: [timeToMilliseconds(param.classTime[0]), timeToMilliseconds(param.classTime[1])],
        teacher: {
          _id: param.teacherId
        },
        price: _l.toNumber(param.price)
      };
      let clasList = await classModel.list({
        classDate: params.classDate,
        status: [1, 2],
        teacherId: params.teacherId
      });
      if (hasOverlap(params, clasList.records)) {
        resolve({});
        return;
      }
      const result = await classModel.add(params);
      resolve(result);
    } catch (error) {
      console.log(error);
      reject(error);
    };
  })
}

function edit(param) {
  return new Promise(async (resolve, reject) => {
    try {
      let params = {
        classStudent: [],
        ...param,
        classDate: dateToTimestamp(param.classDate),
        classTime: [timeToMilliseconds(param.classTime[0]), timeToMilliseconds(param.classTime[1])]
      };
      let classData = await classModel.byId(param._id);
      let clasList = await classModel.list({
        classDate: dateToTimestamp(param.classDate),
        status: [1, 2],
        teacherId: param.teacherId,
        neq_id: param._id
      });
      if (hasOverlap(params, clasList.records)) {
        resolve(false);
        return;
      };
      params.version = classData.version ? classData.version : 0;
      const result = await classModel.edit(params);
      if (result.count >= 1) {
        resolve(true);
      } else {
        resolve(false);
      }
    } catch (error) {
      reject(error);
    };
  })
};

function reservation(param) {
  return new Promise(async (resolve, reject) => {
    try {
      let todo = await classModel.byId(param.classId);
      if (todo.classStudent && todo.classStudent.length >= todo.orderNum) {
        resolve({
          status: false
        });
        return;
      }
      if (!param.studentId) {
        resolve({
          status: false
        });
        return;
      }
      if (todo.classStudent.includes(param.studentId)) {
        resolve({
          status: false
        });
        return;
      }
      todo.classStudent.push(param.studentId);
      const result = await classModel.reservation(todo);
      if (result.count >= 1) {
        resolve({
          status: true
        });
      } else {
        resolve({
          status: false
        });
      }
    } catch (error) {
      reject(error);
    };
  })
};

function cancel(param) {
  return new Promise(async (resolve, reject) => {
    try {
      let todo = await classModel.byId(param.classId);
      if (!param.studentId) {
        resolve({
          status: false
        });
        return;
      }
      if (!todo.classStudent.includes(param.studentId)) {
        resolve({
          status: false
        });
        return;
      }
      todo.classStudent = todo.classStudent.filter(item => {
        return item != param.studentId;
      });
      const result = await classModel.cancel(todo);
      if (result.count >= 1) {
        resolve({
          status: true
        });
      } else {
        resolve({
          status: false
        });
      }
    } catch (error) {
      reject(error);
    };
  })
};

module.exports = {
  list,
  byId,
  add,
  edit,
  reservation,
  cancel
}